package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.PeopledbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by luojingxing on 2019/8/4.
 */

public class PeopledbService {

    //插入或者更新数据库
    public static boolean saveOrUpdate(PeopledbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //分页获取数据
    public static List<PeopledbModel> getData(String userid, int type, int pageIndex, int pageRecord){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(PeopledbModel.class).where("userid","==",userid).and("type","==",type).orderBy("time", true).offset(pageIndex).limit(pageRecord).findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //获取单条数据
    public static PeopledbModel getOneData(String userid, String fromuserid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(PeopledbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).orderBy("time", true).findFirst();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //删除全部数据
    public static boolean deleteAllData(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(PeopledbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除单条数据
    public static boolean deleteById(int id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(PeopledbModel.class, id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

}
